package com.qk.management.controller;

import com.qk.common.Result;
import com.qk.common.util.AliYunOSSOperators;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/upload")
public class FileHanderController {
    @Autowired
    private AliYunOSSOperators oss;
    @PostMapping
    public Result upload(MultipartFile image) {
        Objects.requireNonNull(image);
        Objects.requireNonNull(image.getOriginalFilename());
        try {
            return Result.success(oss.upload(image.getBytes(),UUID.randomUUID()+ image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf( "."))));
        }catch (Exception e){
            log.error("上传失败",e);
            return Result.error("上传失败");
        }
    }
}
